package com.sxz.sbs.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.app.App;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.event.AgainLoginEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.home.HomeActivity;
import com.sxz.sbs.module.login.LoginActivity;
import com.sxz.sbs.rx.RxSubscriber;
import com.sxz.sbs.rx.RxUtil;
import com.sxz.sbs.widget.StallAlertDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * 基类activity
 *
 * @author tian
 */
public abstract class BaseActivity<T extends BasePresenter> extends SupportActivity implements BaseView {

    private Unbinder unBinder;
    public T mPresenter;
    /**
     * 页面是否退出
     */
    public boolean mIsLive = true;
    public Context mContext;
    protected CompositeDisposable mCompositeDisposable;
    private StallAlertDialog loginDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mContext = this;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        unBinder = ButterKnife.bind(this);
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        App.getInstance().addActivity(this);
        init();
    }

    /**
     * 初始化
     */
    protected abstract void init();

    /**
     * 获取布局
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();

    /**
     * 创建Presenter
     *
     * @return Presenter
     */
    protected abstract T createPresenter();

    /**
     * @param targetClazz 目标activity
     */
    public void startActivity(Class<?> targetClazz) {
        Intent intent = new Intent(this, targetClazz);
        startActivity(intent);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showError(int errorTag, String msg) {
        ToastUtils.showShort(msg);
    }

    /**
     * 重新登录
     */
    @Override
    public void loginAgain() {
        mCompositeDisposable = new CompositeDisposable();
        App.preferences.setValue(AppConstant.Preference.TOKEN, null);
        loginDialog = new StallAlertDialog.Builder(this).setMessage("需要重新登陆吗").setCancelable(false).setPositiveButton(new StallAlertDialog.OnPositiveListener() {
            @Override
            public void onPositiveClick(View view) {
                if (!TextUtils.isEmpty(App.preferences.getValue(AppConstant.Preference.TOKEN))) {
                    return;
                }
                HashMap<String, Object> map = HttpParamMap.getParamMap();
                map.put("account", App.preferences.getValue(AppConstant.Preference.USER_NAME));
                map.put("password", App.preferences.getValue(AppConstant.Preference.USER_PSW));
                mCompositeDisposable.add(App.getInstance().getApi().login(map)
                        .compose(RxUtil.<String>handleResult())
                        .subscribeWith(new RxSubscriber<String>((BaseView) mContext) {
                            @Override
                            public void onNext(String token) {
                                App.preferences.setValue(AppConstant.Preference.TOKEN, token);
                                Intent intent = new Intent(mContext, HomeActivity.class);
                                startActivity(intent);
                                EventBus.getDefault().post(new AgainLoginEvent());
                            }

                        }));
            }
        }).setNegativeButton(new StallAlertDialog.OnNegativeListener() {
            @Override
            public void onNegativeClick(View view) {
                startActivity(LoginActivity.class);
                App.getInstance().clearActivity();
                App.preferences.setValue(AppConstant.Preference.TOKEN, null);
            }
        }).create();
        loginDialog.show();

    }

    @Override
    public void showError(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void useNightMode(boolean isNight) {
    }

    @Override
    protected void onDestroy() {
        mIsLive = false;
        if (loginDialog != null && loginDialog.isShowing()) {
            loginDialog.dismiss();
        }
        super.onDestroy();
        mContext = null;
        if (unBinder != null) {
            unBinder.unbind();

        }
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
        App.getInstance().removeActivity(this);
    }
}
